package com.glory.dgcpcpda.activity.mfg;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.ChangeShiftsTestAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.BinInfoEntity;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: ShiftExchangeTestActivity
 * Author: XJH
 * Date: 2021/6/24 15:58
 * Description:
 * History:
 */
public class ShiftExchangeTestActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etShift)
    FreeUI_ClearEditText etShift;
//    @BindView(R.id.etComment)
//    FreeUI_ClearEditText etComment;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.tvClear)
    TextView tvClear;
    @BindView(R.id.list_tv8)
    TextView listTv8;
    @BindView(R.id.list_btn)
    Button listBtn;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
//    @BindView(R.id.tvGetData)
//    TextView tvGetData;
    //良品数
    @BindView(R.id.etFinishNum)
    FreeUI_ClearEditText etFinishNum;
    //不良品数
    @BindView(R.id.tvDefectNum)
    FreeUI_ClearEditText tvDefectNum;
    //良率
    @BindView(R.id.tvDefect)
    TextView tvDefect;

    @BindView(R.id.tvLocation)
    TextView tvLocation;

    @BindView(R.id.tvReportNum)
    TextView tvReportNum;

    @BindView(R.id.tvRemainNum)
    TextView tvRemainNum;

    @BindView(R.id.etClassType)
    TextView etClassType;

    private List<BinInfoEntity.BinInfo> hbinData2;

    private ChangeShiftsTestAdapter mChangeShiftsAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange_test;
    }

    @Override
    protected void initView() {
        tvTitle.setText("产量统计(交班)");
        setMainLayout(llMain);
        listTv1.setText("设备");
        listTv2.setText("位置号");
        listTv3.setText("批号");
        listTv4.setText("良品数");
        listTv5.setText("不良品数");
        listTv6.setText("良率");
        listTv7.setText("技术员");
        listTv8.setText("班次类型");
        listTv1.setVisibility(View.VISIBLE);
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        listTv8.setVisibility(View.VISIBLE);
        listBtn.setVisibility(View.VISIBLE);
        initRecyclerView(rvLotInfoList,this);
        tvBaseSubmit.setText("添加");
        tvDefect.setText("0%");
        etFinishNum.setText("0");
        //控件监听
        setEditListener(etFinishNum,"Y");
        tvDefectNum.setText("0");
        //控件监听
        setEditListener(tvDefectNum,"N");
//        //栏位禁止输入
//        etFinishNum.setEnabled(false);
//        tvDefectNum.setEnabled(false);
        mChangeShiftsAdapter = new ChangeShiftsTestAdapter(null);
        rvLotInfoList.setAdapter(mChangeShiftsAdapter);

        etShift.setShowSoftInputOnFocus(false);
        etUser.setShowSoftInputOnFocus(false);
        etEqp.setShowSoftInputOnFocus(false);
        //隐藏键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
        //隐藏

        new Handler().postDelayed(() -> imm.hideSoftInputFromWindow(etShift.getWindowToken(), 0), 200);

        etShift.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // 获取软键盘的显示状态
                boolean isOpen=imm.isActive();
                //判断是否失去焦点
                if(!hasFocus){
                    //判断是否打开软键盘
                    if (isOpen){
                        imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                else {
                    //隐藏
                    new Handler().postDelayed(() -> imm.hideSoftInputFromWindow(etShift.getWindowToken(), 0), 200);
                }
            }
        });

        etUser.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // 获取软键盘的显示状态
                boolean isOpen=imm.isActive();
                //判断是否失去焦点
                if(!hasFocus){
                    //判断是否打开软键盘
                    if (isOpen){
                        imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                else {
                    //隐藏
                    new Handler().postDelayed(() -> imm.hideSoftInputFromWindow(etUser.getWindowToken(), 0), 200);
                }
            }
        });

        etEqp.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // 获取软键盘的显示状态
                boolean isOpen=imm.isActive();
                //判断是否失去焦点
                if(!hasFocus){
                    //判断是否打开软键盘
                    if (isOpen){
                        imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                else {
                    //隐藏
                    new Handler().postDelayed(() -> imm.hideSoftInputFromWindow(etEqp.getWindowToken(), 0), 200);
                }
            }
        });

        initEdit(etEqp, s ->{
            if(!s.startsWith("FT")){
                etEqp.setText("");
                showConfirmDialog("请输入FT设备编号");
                return;
            }
            LotUtils.searchEqpInfo(getText(etEqp), ShiftExchangeTestActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                tvLocation.setText(response.getEQUIPMENT().getLOCATION());
            }
            @Override
            public void onFail(Throwable t) {
                loadError(t,"EQPQUERY");
            }
        });
        });
        initEdit(etLot, s ->{
            FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                    (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        if(!response.body().getResponse().getBody().getDISPATCHQTY().equals("")){
                            tvRemainNum.setText(response.body().getResponse().getBody().getDISPATCHQTY());
                        }
                        if(!response.body().getResponse().getBody().getDISPATCHEDQTY().equals("")){
                            tvReportNum.setText(response.body().getResponse().getBody().getDISPATCHEDQTY());
                        }
//                        List<ChangeShiftEntity.ChangeShift> changeShiftEntity= response.body().getResponse().getBody().getTESTCHANGESHIFTLINELIST();
//                        int num = 0;//当前批次已提报数量
//                        if(changeShiftEntity.size()>0){
//                            for(ChangeShiftEntity.ChangeShift item : changeShiftEntity){
//                                num = num + item.getGOODQTY() + item.getBADQTY();
//                            }
//                        }
//                        tvReportNum.setText(String.valueOf(num));
//                        if(response.body().getResponse().getBody().getDISPATCHQTY().equals("")){
//                            tvRemainNum.setText("");
//                        }else {
//                            tvRemainNum.setText(String.valueOf(Integer.parseInt(response.body().getResponse().getBody().getDISPATCHQTY())-num));
//                        }

//                        requestLotInfo(s);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"TRANSFERMES");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","LOTSEARCH");
            map.addProperty("LOTID",s);
            NetUtils.requestPostNet("CPC.TESTCHANGESHIFT","TRANSFERMES",this,map,callBack);
            etLot.setFocusable(true);
            etLot.requestFocus();
        });
    }

    @Override
    protected void initData() {
        initEdit2(etShift, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                    (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        if(!response.body().getResponse().getBody().getJOBS().equals("操作员")){
                            etShift.setText("");
                            showConfirmDialog(s+"职位："+response.body().getResponse().getBody().getJOBS()+"，请输入操作员工号");
                            return;
                        }
                        etClassType.setText(response.body().getResponse().getBody().getCLAZZ());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"TESTCHANGESHIFT");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","SELECT");
            map.addProperty("REFEROWNER",s);
            NetUtils.requestPostNet("CPC.TESTCHANGESHIFT","TRANSFERMES",this,map,callBack);
        });
        initEdit2(etUser, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                    (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        if(!response.body().getResponse().getBody().getJOBS().equals("技术员")){
                            etUser.setText("");
                            showConfirmDialog(s+"工号职位："+response.body().getResponse().getBody().getJOBS()+"，请输入技术员工号");
                            return;
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"TESTCHANGESHIFT");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","SELECT");
            map.addProperty("REFEROWNER",s);
            NetUtils.requestPostNet("CPC.TESTCHANGESHIFT","TRANSFERMES",this,map,callBack);
        });
    }


    @OnClick({R.id.tvBaseSubmit,R.id.list_btn,R.id.tvClear})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (getText(etShift).isEmpty()){
                    showToast("请输入作业员",false);
                    return;
                }

                if (etClassType.getText().toString().isEmpty()){
                    showToast("班别不能为空",false);
                    return;
                }

                if (getText(etLot).isEmpty()){
                    showToast("请输入批次号",false);
                    return;
                }

                if (getText(etUser).isEmpty()){
                    showToast("请输入技术员",false);
                    return;
                }

                if (getText(etFinishNum).isEmpty()){
                    showToast("请输入良品数量",false);
                    return;
                }

                if (getText(etEqp).isEmpty()){
                    showToast("请扫描设备号",false);
                    return;
                }

                if (tvLocation.getText().toString().isEmpty()){
                    showToast("设备位置号不能为空",false);
                    return;
                }

                if (getText(etFinishNum).equals("0") && getText(tvDefectNum).equals("0")){
                    showToast("请输入良品和不良品数",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                                    ChangeShiftEntity changeShiftEntity= response.body().getResponse().getBody();
                                    response.body().getResponse().getBody().getTESTCHANGESHIFTLINELIST().get(0).setLOCATION(tvLocation.getText().toString());
                                    mChangeShiftsAdapter.addData(0,response.body().getResponse().getBody().getTESTCHANGESHIFTLINELIST());
                                    mChangeShiftsAdapter.notifyDataSetChanged();
                                    clearContent();
                                    if(!response.body().getResponse().getBody().getSHOWMSG().equals("")){
                                        showConfirmDialog(changeShiftEntity.getSHOWMSG());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TESTCHANGESHIFT");
                            }
                        };

                        JsonObject map = new JsonObject();
                        List<ChangeShiftEntity.ChangeShift> data = new ArrayList<>();
                        map.addProperty("ACTIONTYPE","REFER");
                        ChangeShiftEntity.ChangeShift bean = new ChangeShiftEntity.ChangeShift();
//                bean.setOBJECTRRN("");
//                bean.setCLASSTYPE("");
                        bean.setOPERATORS(getText(etShift));//作业员
                        bean.setTECHNICIAN(getText(etUser));//技术员
                        bean.setGOODQTY(getText(etFinishNum).equals("") ? 0:Integer.parseInt(getText(etFinishNum)));//良品
                        bean.setBADQTY(getText(tvDefectNum).equals("") ? 0:Integer.parseInt(getText(tvDefectNum)));//不良品
                        bean.setGOODRATE(tvDefect.getText().toString());
//                        bean.setREMARKS(getText(etComment));//备注
                        bean.setLOTID(getText(etLot));//批次
                        bean.setCLASSTYPE(etClassType.getText().toString());//班别
                        bean.setEQUIPMENTID(getText(etEqp));//设备
                        data.add(bean);
                        map.add("TESTCHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(data)).getAsJsonArray());
                        NetUtils.requestPostNet("CPC.TESTCHANGESHIFT","TRANSFERMES",this,map,callBack);
                    }
                });
                break;
            case R.id.list_btn:
                if (mChangeShiftsAdapter.deleteData.size() <= 0) {
                    showToast("请勾选批次后，在进行删除操作",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                                    for (ChangeShiftEntity.ChangeShift data : mChangeShiftsAdapter.deleteData){
                                        Iterator<ChangeShiftEntity.ChangeShift> iterator = mChangeShiftsAdapter.getData().iterator();
                                        while (iterator.hasNext()){
                                            if (iterator.next().equals(data)) {
                                                iterator.remove();
                                            }
                                        }
                                    }
                                    mChangeShiftsAdapter.notifyDataSetChanged();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TESTCHANGESHIFT");
                            }
                        };

                        JsonObject map = new JsonObject();
                        List<ChangeShiftEntity.ChangeShift> data = new ArrayList<>();
                        map.addProperty("ACTIONTYPE","DELETE");
                        map.add("TESTCHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(mChangeShiftsAdapter.deleteData)).getAsJsonArray());
                        NetUtils.requestPostNet("CPC.TESTCHANGESHIFT","TRANSFERMES",this,map,callBack);
                    }
                },"是否确认删除");

                break;
            case R.id.tvClear:
                mChangeShiftsAdapter.getData().clear();
                mChangeShiftsAdapter.notifyDataSetChanged();
                clearContent();
                break;
//            case R.id.tvGetData:
//                if (getText(etEqp).equals("")) {
//                    showToast("请先扫描设备信息", false);
//                    return;
//                }
//
//                requestBinInfo();
//                break;
        }


    }

    //请求EAP接口获取BIN1-8数据
//    private void requestBinInfo() {
//        FreeUI_EntityCallBack<BaseResponseBean<BinInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BinInfoEntity>>
//                (new TypeToken<BaseResponseBean<BinInfoEntity>>() {
//                }.getType(), getSupportFragmentManager(), this) {
//
//            @Override
//            public void onSuccess(Response<BaseResponseBean<BinInfoEntity>> response) {
//                super.onSuccess(response);
//                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
//                    if (response.body().getResponse().getBody().getHBINLIST().size() <= 0) {
//                        showToast("暂无数据，请手动输入！",false);
//                        etFinishNum.setEnabled(true);
//                        tvDefectNum.setEnabled(true);
//                        return;
//                    }
//                    List<BinInfoEntity.BinInfo> hbinData = new ArrayList<>();
//                    hbinData2 = new ArrayList<>();
//                    int  FinishNum = 0;
//                    int  DefectNum = 0;
//                    int allNum = 0;
//                    List<BinInfoEntity.BinInfo> hbinlist = response.body().getResponse().getBody().getHBINLIST();
//                    for (BinInfoEntity.BinInfo data : hbinlist){
//                        if (data.getBINNO().equals("BIN1")){
//                            FinishNum = data.getBINQTY();
//                            etFinishNum.setText(String.valueOf(FinishNum));
//                        }else {
//                            DefectNum = DefectNum + data.getBINQTY();
//                        }
//                    }
//                    tvDefectNum.setText(String.valueOf(DefectNum));
//                    allNum = FinishNum + DefectNum;
//                    NumberFormat numberFormat = NumberFormat.getInstance();
//                    numberFormat.setMaximumFractionDigits(3);
//                    String result = numberFormat.format(((float) (FinishNum) / (float) allNum) * 100);
//                    tvDefect.setText(result+"%");
////                    etFinishNum.setEnabled(false);
////                    tvDefectNum.setEnabled(false);
//                    return;
//                }
//                etFinishNum.setText("0");
//                tvDefectNum.setText("0");
//                tvDefect.setText("0%");
////                etFinishNum.setEnabled(true);
////                tvDefectNum.setEnabled(true);
//            }
//
//            @Override
//            public void onError(Response<BaseResponseBean<BinInfoEntity>> response) {
//                super.onError(response);
//                loadError(response.getException(), "EAP.GETHBINREQUEST");
//            }
//        };
//        JsonObject map = new JsonObject();
//        map.addProperty("EQPID", getText(etEqp));
//        map.addProperty("CONNECTIONSTATE", "ONLINE");
//        map.addProperty("LOTID","");
//        NetUtils.requestPostNet("EAP.GETHBINREQUEST", "GETHBINREQUEST", this, map, callBack);
//
//    }


    //获取批次信息
        private void requestLotInfo(String LotID) {
            //获取lot信息
            LotUtils.searchLotInfo(ShiftExchangeTestActivity.this, LotID, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    int remainquantity = 0;
                    remainquantity = Integer.parseInt(response.getLOT().getMAINQTY()) - Integer.parseInt(tvReportNum.getText().toString());
                    tvRemainNum.setText(String.valueOf(remainquantity));
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }

            });

    }

    //初始化页面
    private void clearContent() {
        etShift.setText("");
        etUser.setText("");
        etFinishNum.setText("0");
        tvDefectNum.setText("0");
        tvDefect.setText("0%");
        etEqp.setText("");
        etEqp.setText("");
        etLot.setText("");
        etClassType.setText("");
    }

    //监听良品和不良品数量并计算良率
    private void setEditListener(FreeUI_ClearEditText editText,String type) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {

                    int allNum = 0;
                    if(type.equals("Y")){
                        if(s.length()>10){
                            showToast(s+"数量超限",false);
                            etFinishNum.setText("0");
                            return;
                        }
                        if(s.length()>1){
                            if (s.toString().startsWith("0")){
                                String sub=s.toString().substring(1);
                                etFinishNum.setText(sub);
                                etFinishNum.post(() -> etFinishNum.setSelection(getText(etFinishNum).length()));
                            }
                        }
                        allNum = Integer.parseInt(s.toString()) + (getText(tvDefectNum).equals("") ? 0 : Integer.parseInt(getText(tvDefectNum)));
                        NumberFormat numberFormat = NumberFormat.getInstance();
                        numberFormat.setMaximumFractionDigits(3);
                        String result = numberFormat.format((Integer.parseInt(s.toString())/ (float) allNum) * 100);
                        tvDefect.setText(result.equals("NaN") ? "0%":result+"%");
                    }
                    if(type.equals("N")){
                        if(s.length()>10){
                            showToast(s+"数量超限",false);
                            tvDefectNum.setText("0");
                            return;
                        }
                        if(s.length()>1){
                            if (s.toString().startsWith("0")){
                                String sub=s.toString().substring(1);
                                tvDefectNum.setText(sub);
                                tvDefectNum.post(() -> tvDefectNum.setSelection(getText(tvDefectNum).length()));
                            }
                        }
                        allNum = Integer.parseInt(s.toString()) +(getText(etFinishNum).equals("") ? 0 : Integer.parseInt(getText(etFinishNum)));
                        NumberFormat numberFormat = NumberFormat.getInstance();
                        numberFormat.setMaximumFractionDigits(3);
                        String result = numberFormat.format((getText(etFinishNum).equals("") ? 0 : Integer.parseInt(getText(etFinishNum))/ (float) allNum) * 100);
                        tvDefect.setText(result.equals("NaN") ? "0%":result+"%");
                    }
                }
                else {
                    if(type.equals("Y")){
                        etFinishNum.setText("0");
                    }
                    if(type.equals("N")){
                        tvDefectNum.setText("0");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
